﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Publishing.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.ComponentModel;
using System.Reflection;

namespace Macaw.Moss2007.ContentChangesOverviewSolution
{
    public class StartTimeMenuButton : ToolBarMenuButton, System.Web.UI.IPostBackEventHandler
    {

        public enum EnumAgo
        {
            [Description("All")]
            All,

            [Description("One hour ago")]
            OneHourAgo,

            [Description("One day ago")]
            OneDayAgo,

            [Description("One week ago")]
            OneWeekAgo,

            [Description("Two weeks ago")]
            TwoWeeksAgo,

            [Description("One month ago")]
            OneMonthAgo
        }

        public static String GetEnumDescription(Enum e)
        {
            FieldInfo fieldInfo = e.GetType().GetField(e.ToString());
            DescriptionAttribute[] enumAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (enumAttributes.Length > 0)
            {
                return enumAttributes[0].Description;
            }
            return e.ToString();
        }



        public StartTimeMenuButton()
        {
            this.ID = "StartTimeMenuButton";
        }

        protected override void AddMenuItems()
        {
            foreach (string ago in Enum.GetNames(typeof(EnumAgo)))
            {
                EnumAgo eago = (EnumAgo)Enum.Parse(typeof(EnumAgo), ago);
                this.AddMenuItem(ago, GetEnumDescription(eago), "/_layouts/images/EMPTY.GIF", GetEnumDescription(eago), null, "__doPostBack('" + this.UniqueID + "','" + ago + "')");
            }

            base.AddMenuItems();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();


            base.MenuControl.MenuFormat = MenuFormat.ArrowAlwaysVisible;
            if (!this.Page.IsPostBack)
            {
                Ago = EnumAgo.OneDayAgo;
            }
            else
            {
                Ago = Ago;
            }
        }

        public EnumAgo Ago
        {
            get
            {
                EnsureChildControls();
                return (EnumAgo)this.ViewState["Ago"];
            }
            set
            {
                this.ViewState["Ago"] = value;
                this.MenuControl.CheckedMenuItemIds = value.ToString();
            }
        }


        #region IPostBackEventHandler Members

        void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            EnumAgo eago = (EnumAgo) Enum.Parse(typeof(EnumAgo),eventArgument);
            Ago = eago;
        }

        #endregion
    }
}
